package com.lyncs.ods.modules.goods.controller;


import com.lyncs.ods.common.api.CommonPage;
import com.lyncs.ods.common.api.CommonResult;
import com.lyncs.ods.common.api.Option;
import com.lyncs.ods.modules.goods.model.SkuDetailInfo;
import com.lyncs.ods.modules.goods.service.GoodService;
import com.lyncs.ods.req.ListSkuReq;
import com.lyncs.ods.req.SaveSkuReq;
import com.lyncs.ods.resp.SkuInfoResp;
import com.lyncs.ods.resp.SpuAttrInfoResp;
import com.lyncs.ods.resp.SpuInfoResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * sku信息 前端控制器
 * </p>
 *
 * @author alex
 * @since 2022-02-05
 */
@RestController
@RequestMapping("/goods")
@Api(tags = {"货物相关接口"})
public class GoodController {

    @Autowired
    private GoodService goodService;

    @GetMapping("spu/list")
    @ApiOperation(value = "获取所有spu")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "spu属性名称(模糊搜索)", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页数，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "当前页请求数量，默认20", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<CommonPage<SpuInfoResp>> listSpuInfo(@RequestParam(value = "keyword", required = false) String keyword,
                                                             @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                             @RequestParam(value = "page_size", required = false, defaultValue = "20") Integer pageSize) {
        return CommonResult.success(goodService.listSpu(keyword, page, pageSize));
    }

    @GetMapping("spu/options")
    @ApiOperation(value = "获取所有spu某个属性的候选项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "spu_id", value = "spu id", dataType = "Long"),
            @ApiImplicitParam(name = "attr_id", value = "spu属性ID,-1时表示查询单位候选项", dataType = "Long")
    })
    public CommonResult<List<String>> listSpuOptions(@RequestParam(name = "spu_id") Long spuId,
                                                     @RequestParam(name = "attr_id") Long attrId) {
        return CommonResult.success(goodService.listSpuOptions(spuId, attrId));
    }

    @GetMapping("spu/detail")
    @ApiOperation(value = "根据spu id获取spu属性信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "spu_id", value = "spu id", dataType = "String"),
    })
    public CommonResult<List<SpuAttrInfoResp>> getSpuDetailInfo(@RequestParam(name = "spu_id") Long spuId) {
        return CommonResult.success(goodService.getSpuAttrInfos(spuId));
    }

    @PostMapping("sku/save")
    @ApiOperation(value = "保存或更新sku")
    public CommonResult<Long> saveSku(@Valid @RequestBody SaveSkuReq req) {
        return CommonResult.success(goodService.saveSku(req));
    }

    @PostMapping("sku/list")
    @ApiOperation(value = "根据spu id和属性获取sku信息")
    public CommonResult<CommonPage<SkuDetailInfo>> listSkuInfo(@RequestBody @Valid ListSkuReq req) {
        return CommonResult.success(goodService.listSkuInfo(req));
    }

    @GetMapping("sku/detail")
    @ApiOperation(value = "根据spu id和属性获取sku信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sku_id", value = "skuId", dataType = "Long"),
            @ApiImplicitParam(name = "currency", value = "转换货币", dataType = "Long")
    })
    public CommonResult<SkuDetailInfo> getSkuDetail(@RequestParam("sku_id") Long skuId, @RequestParam(value = "sku_id", required = false) String currency) {
        return CommonResult.success(goodService.getSkuDetail(skuId, currency));
    }

    @GetMapping("sku/refreshAmount")
    @ApiOperation(value = "根据指定skuId和货币刷新sku单价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sku_ids", value = "skuId,多个用英文逗号分割", dataType = "String"),
            @ApiImplicitParam(name = "currency", value = "币种", dataType = "String"),
    })
    public CommonResult<List<Option<Long, BigDecimal>>> refreshSkuAmount(@RequestParam("sku_ids") String skuIds, @RequestParam("currency") String currency) {
        return CommonResult.success(goodService.refreshSkuAmount(skuIds, currency));
    }

    @GetMapping("sku/{skuId}/delete")
    @ApiOperation(value = "删除SKU")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sku_id", value = "sku ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<?> deleteSku(@PathVariable Long skuId) {
        goodService.deleteSku(skuId);
        return CommonResult.success();
    }
}

